package com.zhou.JavaAndroidFirm.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.zhou.JavaAndroidFirm.base.BaseApplication;
import com.zhou.JavaAndroidFirm.utils.DisplayUtil;
import com.zhou.JavaAndroidFirm.R;
import com.zhou.JavaAndroidFirm.activity.NewsDetailActivity;
import com.zhou.JavaAndroidFirm.activity.NewsPhotoDetailActivity;
import com.zhou.JavaAndroidFirm.bean.NewsPhotoDetail;
import com.zhou.JavaAndroidFirm.bean.NewsSummary;
import com.zhou.JavaAndroidFirm.utils.ViewHolderHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2018/7/17 0017
 * @des
 */
public class NewListAdapter extends MultiItemRecycleViewAdapter<NewsSummary> {
    private static final int TYPE_ITEM = 1001;
    private static final int TYPE_PHOTO_ITEM = 1002;

    public NewListAdapter(Context context, List<NewsSummary> datas) {
        super(context, datas, new MultiItemTypeSupport<NewsSummary>() {
            @Override
            public int getLayoutId(int itemType) {
                if (itemType == TYPE_PHOTO_ITEM) {
                    return R.layout.item_news_photo;
                } else {
                    return R.layout.item_news;
                }
            }

            @Override
            public int getItemViewType(int position, NewsSummary newsSummary) {
                if (!TextUtils.isEmpty(newsSummary.getDigest())) {
                    return TYPE_ITEM;
                }
                return TYPE_PHOTO_ITEM;
            }
        });
    }


    @Override
    public void convert(ViewHolderHelper helper, NewsSummary newsSummary) {
        switch (helper.getItemViewType()) {
            case TYPE_ITEM:
                bindItemHolder(helper, newsSummary, getPosition(helper));
                break;
            case TYPE_PHOTO_ITEM:
                bindPhotoItemHolder(helper, newsSummary, getPosition(helper));
                break;
            default:
                break;
        }
    }

    private void bindPhotoItemHolder(ViewHolderHelper holder, final NewsSummary bean, int position) {
        holder.setText(R.id.news_summary_title_tv, bean.getTitle());
        holder.setText(R.id.news_summary_ptime_tv, bean.getPtime());
        setImageView(holder, bean);
        holder.setOnClickListener(R.id.ll_root, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NewsPhotoDetailActivity.startAction(mContext, getPhotoDetail(bean));
            }
        });
    }

    private NewsPhotoDetail getPhotoDetail(NewsSummary bean) {
        NewsPhotoDetail photoDetail = new NewsPhotoDetail();
        photoDetail.setTitle(bean.getTitle());
        setPictures(bean, photoDetail);
        return photoDetail;

    }

    private void setPictures(NewsSummary bean, NewsPhotoDetail photoDetail) {
        List<NewsPhotoDetail.Picture> pictures = new ArrayList<>();
        if (bean.getAds() != null) {
            for (NewsSummary.AdsBean ads : bean.getAds()) {
                setValuesAndAddToList(pictures, ads.getTitle(), ads.getImgsrc());
            }
        } else if (bean.getImgextra() != null) {
            for (NewsSummary.ImgextraBean image : bean.getImgextra()) {
                setValuesAndAddToList(pictures, null, image.getImgsrc());
            }
        } else {
            setValuesAndAddToList(pictures, null, bean.getImgsrc());
        }
        photoDetail.setPictures(pictures);
    }

    private void setValuesAndAddToList(List<NewsPhotoDetail.Picture> pictureList, String title, String imgsrc) {
        NewsPhotoDetail.Picture picture = new NewsPhotoDetail.Picture();
        if (title != null) {
            picture.setTitle(title);
        }
        picture.setImgSrc(imgsrc);

        pictureList.add(picture);
    }

    private void setImageView(ViewHolderHelper holder, NewsSummary bean) {
        int PhotoThreeHeight = DisplayUtil.dip2px(90);
        int PhotoTwoHeight = DisplayUtil.dip2px(120);
        int PhotoOneHeight = DisplayUtil.dip2px(150);

        String imageLeft = null, imageMiddle = null, imageRight = null;
        LinearLayout news_summary_photo_iv_group = holder.getView(R.id.news_summary_photo_iv_group);
        ViewGroup.LayoutParams layoutParams = news_summary_photo_iv_group.getLayoutParams();

        if (bean.getAds() != null) {
            if (bean.getAds().size() >= 3) {
                imageLeft = bean.getAds().get(0).getImgsrc();
                imageMiddle = bean.getAds().get(1).getImgsrc();
                imageRight = bean.getAds().get(2).getImgsrc();
                layoutParams.height = PhotoThreeHeight;
                holder.setText(R.id.news_summary_title_tv, BaseApplication.getAppContext()
                        .getString(R.string.photo_collections, bean.getAds().get(0).getTitle()));
            } else if (bean.getAds().size() >= 2) {
                imageLeft = bean.getAds().get(0).getImgsrc();
                imageMiddle = bean.getAds().get(1).getImgsrc();
                layoutParams.height = PhotoTwoHeight;
            } else if (bean.getAds().size() >= 1) {
                imageLeft = bean.getAds().get(0).getImgsrc();
                layoutParams.height = PhotoOneHeight;
            }
        } else if (bean.getImgextra() != null) {
            int size = bean.getImgextra().size();
            if (size >= 3) {
                imageLeft = bean.getImgextra().get(0).getImgsrc();
                imageMiddle = bean.getImgextra().get(1).getImgsrc();
                imageRight = bean.getImgextra().get(2).getImgsrc();

                layoutParams.height = PhotoThreeHeight;
            } else if (size >= 2) {
                imageLeft = bean.getImgextra().get(0).getImgsrc();
                imageMiddle = bean.getImgextra().get(1).getImgsrc();

                layoutParams.height = PhotoTwoHeight;
            } else if (size >= 1) {
                imageLeft = bean.getImgextra().get(0).getImgsrc();

                layoutParams.height = PhotoOneHeight;
            }
        } else {
            imageLeft = bean.getImgsrc();
            layoutParams.height = PhotoOneHeight;
        }
        setPhotoImageView(holder, imageLeft, imageMiddle, imageRight);

    }

    private void setPhotoImageView(ViewHolderHelper holder, String imageLeft, String imageMiddle, String imageRight) {
        if (imageLeft != null) {
            holder.setVisible(R.id.news_summary_photo_iv_left, true);
            holder.setImageUrl(R.id.news_summary_photo_iv_left, imageLeft);
        } else {
            holder.setVisible(R.id.news_summary_photo_iv_left, true);
        }

        if (imageMiddle != null) {
            holder.setVisible(R.id.news_summary_photo_iv_middle, true);
            holder.setImageUrl(R.id.news_summary_photo_iv_middle, imageMiddle);
        } else {
            holder.setVisible(R.id.news_summary_photo_iv_middle, true);
        }
        if (imageRight != null) {
            holder.setVisible(R.id.news_summary_photo_iv_right, true);
            holder.setImageUrl(R.id.news_summary_photo_iv_right, imageRight);
        } else {
            holder.setVisible(R.id.news_summary_photo_iv_right, false);
        }
    }

    private void bindItemHolder(final ViewHolderHelper helper, final NewsSummary newsSummary, int position) {
        helper.setText(R.id.news_summary_title_tv, !TextUtils.isEmpty(newsSummary.getLtitle()) ? newsSummary.getLtitle() : newsSummary.getTitle());
        helper.setText(R.id.news_summary_ptime_tv, newsSummary.getPtime());
        helper.setText(R.id.news_summary_title_tv, newsSummary.getDigest());
        helper.setImageUrl(R.id.news_summary_photo_iv, newsSummary.getImgsrc());
        helper.setOnClickListener(R.id.rl_root, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NewsDetailActivity.startAction(mContext, helper.getView(R.id.news_summary_photo_iv), newsSummary.getPostid(), newsSummary.getImgsrc());
            }
        });
    }


}

